from datetime import datetime

from loguru import logger

from src.detection.const import ENotifyTypeUI
from src.mode.detection.server import PlatformDevice, SmartEnergyUnit
from src.mode.meter.server import MeterDevice
from src.mode.peripheral.server import PeripheralDevice
from src.mode.support import Support


def notify_ui(msg: str, custom_style: bool = False):
    logger.info("UI_NOTIFY: 日志类型-{}，日志数据-{}".format(ENotifyTypeUI.NOTIFY_LOG, msg))
    if custom_style:
        skip = logger.add("record_log.log", format="{message}", level="INFO")
    else:
        skip = logger.add("record_log.log", format="{time}  {level}  {message}", level="INFO")
    logger.info(msg)
    logger.remove(skip)


class TestCommonHuaYun:
    def __init__(self, platform: PlatformDevice, energy_unit: SmartEnergyUnit, meter: MeterDevice,
                 peripheral: PeripheralDevice, support: Support, param_data: dict, param_gist: tuple, exec_notify):
        self.energy_unit = energy_unit
        self.meter = meter
        self.platform = platform
        self.peripheral = peripheral
        self.support = support
        self.param_data = param_data
        self.param_gist = param_gist
        self.notify = notify_ui
        self.exec_notify = exec_notify


class StepLogAndPrint:
    def __init__(self, e):
        self.sub_step = []
        self.esam = ""
        self.exec_notify = e

    def append_success_step(self, title):
        self.show_last_step()
        self.sub_step.append([title, 1])
        self.exec_notify(ENotifyTypeUI.NOTIFY_LOG, title, True)

    def append_error_step(self, title):
        self.show_last_step()
        self.sub_step.append([title, 0])
        self.exec_notify(ENotifyTypeUI.NOTIFY_LOG, title, True)

    def show_last_step(self):
        record_num = len(self.sub_step)
        if record_num > 0:
            step_record = self.sub_step[record_num - 1]
            if step_record[1]:
                print("步骤 " + step_record[0])
            else:
                print("步骤 " + step_record[0])

    def finish(self, start_time: datetime):
        localtime = datetime.now()
        second_diff_num = (localtime - start_time).total_seconds()
        print("测试结束，耗时", second_diff_num)
        print("测试结果：")
        success_count = 0
        failed_count = 0
        step_num = 0
        for record in self.sub_step:
            step_num += 1
            if record[1]:
                success_count += 1
                print("第" + str(step_num) + "步:" + record[0])
                self.exec_notify(ENotifyTypeUI.NOTIFY_LOG, "第" + str(step_num) + "步:" + record[0])
            else:
                failed_count += 1
                print("第" + str(step_num) + "步:" + record[0])
                self.exec_notify(ENotifyTypeUI.NOTIFY_LOG, "第" + str(step_num) + "步:" + record[0])

    def append_result(self, success: bool, log=""):
        num = len(self.sub_step)
        if num == 0:
            self.sub_step.append(["", False, ""])
            num = 1

        self.sub_step[num - 1][1] = success
        if len(self.sub_step[num - 1][2]) > 0:
            self.sub_step[num - 1][2] += ";"
        self.sub_step[num - 1][2] += log

